package com.crm.customers.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crm.customers.dao.CustomerClaimDao;
import com.crm.customers.model.CustomerClaim;
import com.crm.customers.model.CustomerRecyclebin;
import com.crm.customers.service.CustomerClaimService;
import com.crm.customers.service.CustomerService;
import com.crm.im.core.model.Message;
import com.crm.sys.model.Log;
import com.crm.sys.service.PushService;
import com.crm.sys.service.SysService;
import com.crm.user.service.UserService;
import com.crm.util.Constants;
@Service("customerClaimService")
public class CustomerClaimServiceImpl implements CustomerClaimService {

	@Autowired
	private CustomerClaimDao customerClaimDao;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private PushService pushService;
	@Autowired
	private SysService sysService;
	@Autowired
	private UserService userService;
	
	@Override
	public List<CustomerClaim> getCustomerClaimList(CustomerClaim cc,
			int pageNo, int pageSize) {
		cc.setStartsize((pageNo-1)*pageSize);
		cc.setEndsize(pageSize);
		return customerClaimDao.getCustomerClaimList(cc);
	}

	@Override
	public int saveCustomerClaim(CustomerClaim cc) {
		int result = 0;
	    if(cc.getId()!=null){
	    	
	    	CustomerClaim ccObj = new CustomerClaim();
    		ccObj.setId(cc.getId());
    		ccObj =  customerClaimDao.getCustomerClaimInfo(ccObj);
    		if(ccObj.getCusid()!=null){
    			String msg = "索要被驳回，驳回理由:"+cc.getOpinion();
		    	result = customerClaimDao.updateCustomerClaim(cc);
		    	//同意索要资源
		    	if(result>0&&cc.getState()==1){
		    		customerService.updatecustomeruid(cc.getUpdateuser(),ccObj.getTouid(), ccObj.getCusid(),cc.getUpdateuser(),ccObj.getCustomerInfo().getType());
		    		msg = "索要成功,请查看对应资源库";
		    		//索要成功后，将记录存入log中
		    		Log log = new Log();
		    		log.setContentid(ccObj.getCustomerInfo().getId());
		    		if (ccObj.getCustomerInfo().getType() == 0) {
		    			log.setCategory(Constants.LOGCATEGORY.ZERO);
		    		} else {
		    			log.setCategory(Constants.LOGCATEGORY.ONE);
		    		}
		    		log.setUid(cc.getUpdateuser());
		    		log.setOid(ccObj.getCreateuser());
		    		log.setDescription(ccObj.getCreateuserName() + "索要了客户" + ccObj.getCustomerInfo().getCname()+","+msg);
		    		log.setState(Constants.LOGSTATE.FOUR);
		    		sysService.saveLoginfo(log);
		    	}
		    	Message message = new Message();
				message.setType("2");
				message.setContent(ccObj.getCustomerInfo().getCname()+msg);
				message.setReceiver(String.valueOf(ccObj.getTouid()));
				pushService.pushMsgToUser(message);
				userService.sendMsgToQQ(ccObj.getTouid(), ccObj.getCustomerInfo().getCname()+msg);
    		}
	    }else{
	    	cc.setState(0);
	    	cc.setCreateuser(cc.getUpdateuser());
	    	cc.setTouid(cc.getUpdateuser());
	    	result = customerClaimDao.insertCustomerClaim(cc);
	    }
		return result;
	}

}
